﻿using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;

namespace Chenglin.WPJigsawPuzzle
{
    /// <summary>
    /// Store the state of the game.
    /// </summary>
    [XmlRoot("state")]
    public class GameState
    {
        private const string StateFileName = "state.xml";
        private static GameState s_state;
        private static object s_locker = new object();
        private static XmlSerializer s_serializer = new XmlSerializer(typeof(GameState));

        /// <summary>
        /// Get or set the position of the default puzzle image.
        /// </summary>
        [XmlAttribute("img")]
        public int ImagePos
        {
            get;
            set;
        }

        /// <summary>
        /// constructor
        /// </summary>
        public GameState()
        { }

        /// <summary>
        /// Get the state of the application.
        /// </summary>
        public static GameState State
        {
            get
            {
                if (s_state == null)
                {
                    ReadStateFile();
                }
                return s_state;
            }
        }

        /// <summary>
        /// Read the state of the game from the storage.
        /// </summary>
        public static void ReadStateFile()
        {
            lock (s_locker)
            {
                if (s_state == null)
                {
                    using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (storage.FileExists(StateFileName))
                        {
                            using (var stream = storage.OpenFile(StateFileName, FileMode.Open, FileAccess.Read))
                            {
                                try
                                {
                                    s_state = s_serializer.Deserialize(stream) as GameState;
                                }
                                catch
                                {
                                    // Ignore all exceptions
                                }
                            }
                        }
                    }
                }

                if (s_state == null)
                {
                    s_state = new GameState();
                }
            }
        }

        public static void SaveStateFile()
        {
            GameState state = s_state;
            if (state == null)
            {
                return;
            }

            using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var stream = storage.OpenFile(StateFileName, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    try
                    {
                        s_serializer.Serialize(stream, state);
                    }
                    catch
                    {
                        // Ignore all exceptions
                    }
                }
            }
        }
    }
}
